package com.cjzuji.rental.dao.impl;

import com.cjzuji.rental.dao.AiDouOrderDao;
import com.cjzuji.rental.mapper.OrderContractMapper;
import com.cjzuji.rental.mapper.OrderMapper;
import com.cjzuji.rental.mapper.OrderUserMapper;
import com.cjzuji.rental.pojo.Orders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
@Service
public class AiDouOrderDaoImpl implements AiDouOrderDao {
    private static final Logger logger = LoggerFactory.getLogger(AiZuJiOrderDaoImpl.class);
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderUserMapper orderUserMapper;
    @Autowired
    OrderContractMapper orderContractMapper;

    @Override
    @Transactional
    public Long save(Map<String, Object> data, Map<String, Object> detail,Double outBuyAmount) {
        int idCardStatus = 0;
        if (!data.get("idCard").toString().equals("")) {
            idCardStatus = 1;
        }
        int orderStatus;
        String deliveryNo = "";
        switch (data.get("orderStatus").toString()) {
            case "TO_BE_RETURNED":  //租用中
                orderStatus = 3;
                deliveryNo = detail.get("logisticsNo").toString();
                break;
            case "OVERDUE":  //逾期
                orderStatus = 4;
                deliveryNo = detail.get("logisticsNo").toString();
                break;
            case "COMPLETED":  //完成
                orderStatus = 5;
                deliveryNo = detail.get("logisticsNo").toString();
                break;
            case "UNDELIVERED":
                orderStatus = 1;
                break;
            case "TO_BE_RECEIVED":
                orderStatus = 2;
                break;
            default:
                orderStatus = 7;
                break;
        }
        int screenRisksType = 0;
        if(Double.parseDouble(detail.get("screenInsure").toString()) > 0){
            screenRisksType = 1;
        }
        Double surplusBondAmt = 0.0;
        if(detail.containsKey("secondDeposit")&&detail.get("secondDeposit") != null){
            surplusBondAmt = Double.parseDouble(detail.get("secondDeposit").toString());
        }
        String assigner = "";
        if(data.containsKey("riskManNickName")&&data.get("riskManNickName") != null){
            assigner = data.get("riskManNickName").toString();
        }
        Double monthRepayment =0.0;
        if(detail.containsKey("amountTerm")&&detail.get("amountTerm") != null){
            monthRepayment = Double.parseDouble(detail.get("amountTerm").toString());
        }
        String rentStartTime = "";
        String rentLastTime = "";
        if(detail.containsKey("startLeaseTerm")&&detail.get("startLeaseTerm") != null){
            rentStartTime = detail.get("startLeaseTerm").toString();
        }
        if(detail.containsKey("finishLeaseTerm")&&detail.get("finishLeaseTerm") != null){
            rentLastTime = detail.get("finishLeaseTerm").toString();
        }
        String phone = "";
        if(data.get("consigneeMobile") != null){
            phone = data.get("consigneeMobile").toString();
        }
        String spec = data.get("goodsName").toString();
        String[] parts = spec.split("\\s+");
        String color = "";
        String sku = "";
        if(parts[parts.length - 1] == "典藏版"){
            color = parts[parts.length - 3];
            sku = parts[parts.length - 2];
        }else {
            color = parts[parts.length - 2];
            sku = parts[parts.length - 1];
        }
        String opinion = "";
        if(data.containsKey("zuKaRiskRank")&&data.get("zuKaRiskRank") != null){
            opinion = data.get("zuKaRiskRank").toString();
        }
        Double discountAmount = 0.0;
        if(data.containsKey("couponPrice")&&data.get("couponPrice") != null){
            discountAmount = Double.parseDouble(data.get("couponPrice").toString());
        }
        Orders aiDouOrder = Orders.builder()
                .shopId(4)
                .platformId(6)
                .orderId(data.get("sn").toString())
                .realName(data.get("authName").toString())    //下单用户
                .phone(phone)   //下单用户手机号
                .opinion(opinion)  //风控
                .assigner(assigner)  //分配人 备注人
                .orderIdTag(data.get("id").toString()) //平台订单ID
                .carBrand(spec) //产品备注
                .colour(color)  //商品属性
                .sku(sku)  //商品SKU
                .actualFiancingAmount(Double.parseDouble(detail.get("grossRent").toString()))  //总租金
                .buyoutAmount(outBuyAmount)  //买断价
                .discountAmount(discountAmount)   //优惠金额
                .monthRepayment(monthRepayment)  //月租金
                .bondFreeCreditAmt(Double.parseDouble(detail.get("signedPrice").toString())) //订单总金额  signedPrice
                .surplusBondAmt(surplusBondAmt) //押金
                .leaseTerm(data.get("leaseTerm").toString())   //订单租期
                .idAuthStatus(idCardStatus)  //身份证上传状态
                .rentStartTime(rentStartTime)  //租赁开始时间
                .rentLastTime(rentLastTime)  //租赁结束时间
                .status(orderStatus)    //订单状态
                .deliveryNo(deliveryNo)  //快递单号
                .createTime(data.get("createTime").toString())  // 下单时间
                .updateTime(new Date())
                .screenRisksAmount(Double.parseDouble(detail.get("screenInsure").toString())) //碎屏金额   真实支付的碎屏价格
                .screenRisksType(screenRisksType)//碎屏状态
                .build();
        try {
            orderMapper.insert(aiDouOrder);
            return aiDouOrder.getId();
        }catch (Exception e){

            logger.info("添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }

}